<?php
namespace app\spadmin\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\model\ShopOrder;
use app\common\model\ShopProcess;
use app\common\model\User;
use app\common\model\TradeAccount;
use app\common\model\UserBrokerageLog;
use think\Db;
use app\common\library\Matching;
use app\common\model\SceneCode;
class Order extends Command
{
     protected function configure()
    {
        $this->setName('orderCommand')->setDescription('order expire and order auto finished ');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->orderOverdue();
        $this->orderfinished();
        $output->writeln('end');
    }
    /**
     * 订单支付过期
     */
    protected function orderOverdue (){

        ShopOrder::where('status',1)->where('pay_type',1)->where('expire_time','<=',time())->delete();
    }
    /***
     * 订单自动完成
     */
    protected function orderfinished(){
        $shopOrder = Db::name('ShopOrder')->where('status',4)->where('is_refund',0)->where('express_time','<',time())->field('id')->select();
        if(!empty($shopOrder)){
            foreach($shopOrder  as $key =>$value){
                $this->order($value['id']);
            }
            
        }
    }

    protected function order($id){
        $order = ShopOrder::get($id);
        $companyAll = $order['company_price']*$order['goods_num'];
        Db::startTrans();
        try{
            $order->status = 6;
            if($order->type == 3){
                $order->brokerage_num = 0;
            }else{
                $order->brokerage_num = $order->order_price - $companyAll;
            }
            $order->save();
            if($order->type == 3){
                ShopProcess::create([
                    'order_id'=>$order->id,
                    'process_remark'=>'系统自动完成积分订单',
                    'status'=>6
                ]);
            }else{
                ShopProcess::create([
                    'order_id'=>$order->id,
                    'process_remark'=>'系统自动完成订单,分润总额：'.$order->brokerage_num.'元',
                    'status'=>6
                ]);
            }
            if($order->type != 3){
                TradeAccount::create([
                    'channel'=>1,
                    'trade_account'=>$order->order_price
                ]);
            }
            TradeAccount::create([
                'channel'=>2,
                'trade_account'=>$order->base_price*$order->goods_num
            ]);
            if($order->s_id>0){
                $brokerage = Matching::orderBalance($order->brokerage_num,$order->s_level_id);
                $suser = User::where('id',$order->s_id)->find();
                ShopOrder::where('id',$order->id)->update(['user_brokerage' => $brokerage]);
                UserBrokerageLog::create([
                    'channel'=>1,
                    'type'=>2,
                    'user_id'=>$order->s_id,
                    'order_id'=>$order->id,
                    'year'=>date('Y'),
                    'month'=>date('m'),
                    'before_price'=>$suser->brokerage,
                    'price'=>$brokerage,
                    'change_price'=>'+'.$brokerage,
                    'after_price'=>$suser->brokerage+$brokerage,
                    'remark'=>'商务采购分润:'.$brokerage
                ]);
                User::where('id',$order->s_id)->setInc('brokerage',$brokerage);
                TradeAccount::create([
                    'channel'=>2,
                    'trade_account'=>$brokerage
                ]);
                if($order->p_id>0){
                    $pbrokerage = substr(sprintf("%.3f", $brokerage*0.3),0,-1);
                    $puser = User::where('id',$order->p_id)->find();
                    UserBrokerageLog::create([
                        'channel'=>1,
                        'type'=>2,
                        'user_id'=>$order->p_id,
                        'order_id'=>$order->id,
                        'year'=>date('Y'),
                        'month'=>date('m'),
                        'before_price'=>$puser->brokerage,
                        'price'=>$pbrokerage,
                        'change_price'=>'+'.$pbrokerage,
                        'after_price'=>$puser->brokerage+$pbrokerage,
                        'remark'=>'商务采购下级分润:'.$pbrokerage
                    ]);
                    User::where('id',$order->p_id)->setInc('brokerage',$pbrokerage);
                    TradeAccount::create([
                        'channel'=>2,
                        'trade_account'=>$pbrokerage
                    ]);
                    if($order->t_id>0){
                        $tbrokerage = substr(sprintf("%.3f", $brokerage*0.1),0,-1);
                        $tuser = User::where('id',$order->t_id)->find();
                        UserBrokerageLog::create([
                            'channel'=>1,
                            'type'=>2,
                            'user_id'=>$order->p_id,
                            'order_id'=>$order->id,
                            'year'=>date('Y'),
                            'month'=>date('m'),
                            'before_price'=>$tuser->brokerage,
                            'price'=>$tbrokerage,
                            'change_price'=>'+'.$tbrokerage,
                            'after_price'=>$tuser->brokerage+$pbrokerage,
                            'remark'=>'商务采购下下级分润:'.$tbrokerage
                        ]);
                        User::where('id',$order->t_id)->setInc('brokerage',$tbrokerage);
                        TradeAccount::createcreate([
                            'channel'=>2,
                            'trade_account'=>$tbrokerage
                        ]);
                    }
                }
            }
            Db::commit();
        }catch( Exception $e){
            Db::rollback();
        }
    }
}